<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:圖片上傳.
 *@param datetime 2011-10-18
 *@param author solgh
 ********************/
class Upload extends CI_Controller{
  private $data=array();
  
  function __construct(){
    parent::__construct();
  }
  
   //批量上傳
  public function bulk_uploads(){
      $folder=$this->input->post('folder');
      $file_forder = 'uploads/'.$folder.'/';                      //文件目录
      Imagelib::mkdir_file($file_forder);
      $file_name  = md5(uniqid(mt_rand()));  //新文件名
      $config['upload_path'] = FCPATH.$file_forder;           //文件保存路径  这儿我用的是实际路径
      $config['allowed_types'] = 'jpg|jpeg|gif|png|bmp';  //允许上传格式
      $config['max_size'] = '20480000';                           //允许上传大小
      $config['file_name']= $file_name;                  //存放的文件名
      $this->load->library('upload', $config);
      $field_name = 'Filedata';   //上传表单字段名
      if ($this->upload->do_upload($field_name)){
        $data   = $this->upload->data();
        $img_path =$file_forder.$data['file_name']; //圖片地址
        //返回给SWF AND JQUERY显示的IMAGE地址 生成縮略圖.      
        $thumb_img=base_url().Imagelib::resize_thumb($file_forder.$data['file_name'],150,150);
        $img_data=array(
          'delete_url'=>site_url('admins/upload/delete').'?path='.$img_path,
          'img_path'=>$img_path,
          'thumb_img'=>$thumb_img,
        ); 
        echo json_encode($img_data); 
        exit;
      }
  }
     
  //刪除圖片
  public function delete(){
    $path=$this->input->get('path');
    if(!empty($path)){
      Imagelib::delete_image($path);
      Imagelib::delete_thumb_image($path);
      echo '1';
    }else{
      echo 'File does not exist!';
    }
  }

  //單個上傳
  public function single_upload(){

    $file_forder = $this->input->post('folder');//文件目录
    $_element_id = $this->input->post('_element_id'); //對應的替換jquery ID
    $_admin_id = $this->input->post('_admin_id'); //用戶id
    $_nickName = $this->input->post('_nickName'); //用戶名
    $_size = $this->input->post('_size') ; // 獲取縮圖的尺寸 190x190格式
    $_size_arr=!empty($_size)?explode('x',$_size):''; //拆成數組

    if(empty($_admin_id) || empty($_nickName)) show_404();

    $login_name=$this->nsession->userdata('nick_name');

    if($login_name==$_nickName){ //驗證令牌
      header('content-Type: application/json; charset=UTF-8', true);
      $file_forder = !empty($file_forder)?urldecode($file_forder):'uploads/single/';

      Imagelib::mkdir_file($file_forder);
      $file_name  = md5(uniqid(mt_rand()));  //新文件名
      $config['upload_path'] = FCPATH.$file_forder;           //文件保存路径  这儿我用的是实际路径
      $config['allowed_types'] = 'jpg|jpeg|gif|png|bmp|pdf|txt|zip|xls|doc|docx|xlsx|apk';  //允许上传格式
      $config['max_size'] = 102400000;                           //允许上传大小1024*10 10M
      $config['file_name']= $file_name;                  //存放的文件名
      $this->load->library('upload', $config);
      $field_name = 'Filedata';   //上传表单字段名
      if ($this->upload->do_upload($field_name)){
          $data   = $this->upload->data();
          $img_path =$file_forder.$data['file_name']; //圖片地址
          //返回给SWF AND JQUERY显示的IMAGE地址 生成縮略圖.
          $thumb_img='';
          if(!in_array($_element_id,array('href_link','info_file'))){ //判斷是否為文件
            $thumb_img_size=!empty($_size_arr) && count($_size_arr)==2?Imagelib::resize_thumb($img_path,$_size_arr[0],$_size_arr[1]):Imagelib::resize_thumb($img_path,260,260);
            $thumb_img=base_url().$thumb_img_size;
          }
          $img_data=array(
            'delete_url'=>site_url('admins/upload/delete').'?path='.$img_path,
            'img_path'=>$img_path,
            'element_id'=>$_element_id,
            'thumb_img'=>$thumb_img,
            'http_img'=>base_url().$img_path,
          ); 
          echo json_encode($img_data); 
          exit;
      }else{

        $error=array(
          'error'=>'上傳文件大小超過10M,或者不是'.$config['allowed_types'].'文件格式!',
          'upload_error'=>$this->upload->display_errors(),
        );
        echo json_encode($error);
        exit;
      }  
    }
    show_404();
  }


  //Redactor 
  //redactor file_upload 文件上傳
  public function file_upload(){
    $file_forder = 'uploads/redactor/';//文件目录
    Imagelib::mkdir_file($file_forder);
    $file_name  = md5(uniqid(mt_rand()));  //新文件名
    $config['upload_path'] = FCPATH.$file_forder;   //文件保存路径  这儿我用的是实际路径
    $config['allowed_types'] = '*';  //允许上传格式
    $config['max_size'] = '1024000000';                 //允许上传大小
    $config['file_name']= $file_name;               //存放的文件名
    $this->load->library('upload', $config);
    $field_name = 'Filedata';   //上传表单字段名
    if ($this->upload->do_upload($field_name)){
      $data = $this->upload->data();
      $img_path =$file_forder.$data['file_name']; //圖片地址
      $img_data=array(
        'filelink'=>base_url().$img_path,
        'filename'=>$data['file_name'], //文件上傳需要
        'orig_name' => $data['raw_name'],
      ); 
      echo stripslashes(json_encode($img_data)); 
      exit;
    }else{
      $error=array(
        'error'=>'上傳文件大小超過10M,或者不是'.$config['allowed_types'].'文件格式!000',
        'upload_error'=>$this->upload->display_errors(),
      );
      echo json_encode($error);
      exit;
    }
  }

  //redactor image_upload 圖片
  public function image_upload(){
    $file_forder = 'uploads/redactor/';//文件目录
    Imagelib::mkdir_file($file_forder);
    $file_name  = md5(uniqid(mt_rand()));  //新文件名
    $config['upload_path'] = FCPATH.$file_forder;   //文件保存路径  这儿我用的是实际路径
    $config['allowed_types'] = 'jpg|jpeg|gif|png';  //允许上传格式
    $config['max_size'] = '2048000';                 //允许上传大小
    $config['file_name']= $file_name;               //存放的文件名
    $this->load->library('upload', $config);
    $field_name = 'file';   //上传表单字段名
    if ($this->upload->do_upload($field_name)){
      $data = $this->upload->data();
      $img_path =$file_forder.$data['file_name']; //圖片地址
      Imagelib::resize_thumb($img_path,100,74);
      $img_data=array(
        'filelink'=>base_url().$img_path,
        'location'=>"/".$img_path,
      ); 
      echo stripslashes(json_encode($img_data)); 
      exit;
    }else{

      $error=array(
        'error'=>'上傳文件大小超過10M,或者不是'.$config['allowed_types'].'文件格式!',
        'upload_error'=>$this->upload->display_errors(),
      );
      echo json_encode($error);
      exit;
    }
  }

  //遍歷文件夾 獲取圖片
  public function get_images(){
    $img = array('gif','png','jpg');//所有图片的后缀名
    $dir = FCPATH.'uploads/redactor/';//文件夹名称
    $pic = array();
    foreach($img as $k=>$v){
      $pattern = $dir.'*.'.$v;
      $image_array = glob($pattern);
      if (!empty($image_array)) {
        foreach ($image_array as $path) {
          $image = basename($path);
          $thumb_image=Imagelib::resize_thumb('uploads/redactor/'.$image,100,74);
          $pic[]=array(
          'thumb' => base_url().$thumb_image,
          'image' => base_url('uploads/redactor/'.$image),
          'title' => $image,
          'folder' => 'All'
          );
        }
      }
    }
    echo json_encode($pic);
    exit;
  }
}